﻿using System;
using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// The benchmark insertion tool
    /// Clic the insertion point, then the name of the benchmark, then its altitude if known, or nothing if unknown
    /// </summary>
    public class TBenchmark: BaseArchaeoTool
    {
        #region Fields
        Benchmark Benchmark;
        #endregion

        #region Constructor
        /// <summary>
        /// The tool to draw benchmark objects
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TBenchmark(Viewport Viewport)
            : base(Viewport,"Benchmark")
        {
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            if (Benchmark == null)
            {
                ExternalPointSent(GetCoordinatesFromString(Text));
            }
            else if (Benchmark.Name == "")
            {
                Benchmark.Name = Text;
                SendMessage("Enter benchmark altitude, then validate");
                ShowTextInput();
            }
            else{
                try
                {
                    Benchmark.Altitude = Convert.ToDouble(Text, Core.Vars.Setup.GeoSketch.NumberFormat);
                }
                catch
                {
                    Benchmark.Altitude = 0;
                }
                Viewport.GeoObjects.Add(Benchmark);
                Viewport.Project.SiteBenchmarks.Add(Benchmark);
                Benchmark = null;
                RefreshViewport();
                SendMessage("Click the location of the benchmark");
            }
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            Benchmark = new Benchmark(Point, 0, Viewport.CurrentLayer, Viewport.CurrentExcavationArea,Viewport.CurrentColor);
            RefreshViewport();
            SendMessage("Enter benchmark name, then validate");
            ShowTextInput();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draw the graphics requested by the object
        /// </summary>
        public override void Draw()
        {
            if (Benchmark != null)
            {
                Benchmark.Draw(Viewport);
            }
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click the location of the benchmark");
        } 
        #endregion
    }
}